home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / overview / win2maccountersamples / 5. counterprint / source / ccounterdocument.cp < prev    next >
Encoding:
Text File  |  2000-09-28  |  6.6 KB  |  249 lines

  1. /*
  2.     File:        CCounterDocument.cp
  3.  
  4.     Contains:    Sample code to accompany Chapter 12 of 
  5.                 "An Introduction to Macintosh Programming for Windows Programmers".
  6.                 
  7.     Written by:    Worldwide Developer Technical Support
  8.  
  9.     Copyright:    1999 Apple Computer, Inc., All Rights Reserved
  10.  
  11.       You may incorporate this sample code into your applications without
  12.       restriction, though the sample code has been provided "AS IS" and the
  13.       responsibility for its operation is 100% yours.  However, what you are
  14.       not permitted to do is to redistribute the source as "DSC Sample Code"
  15.       after having made changes. If you're going to re-distribute the source,
  16.      we require that you make it clear in the source that the code was
  17.     descended from Apple Sample Code, but that you've made changes.
  18.     
  19. */
  20. #include <LFile.h>
  21. #include <LPlaceHolder.h>
  22. #include <LPrintout.h>
  23. #include <LString.h>
  24. #include <LWindow.h>
  25. #include <PP_Messages.h>
  26. #include <UMemoryMgr.h>
  27. #include <UWindows.h>
  28. #include <UModalDialogs.h>
  29.  
  30. #include <iostream>                // console for debugging
  31. using namespace std;              //introduces namespace std
  32.  
  33. #include "CounterConstants.h"
  34. #include "CCounterDocument.h"
  35.  
  36. //==================================================================================
  37. CCounterDocument::CCounterDocument(LCommander* inSuper, FSSpec*    inFileSpec)
  38.     : LSingleDoc(inSuper)
  39. {
  40.     mWindow = nil;
  41.     mWindow = MakeDocumentWindow();
  42.     ThrowIfNil_(mWindow);
  43.     if (inFileSpec == nil) {
  44.         NameNewDoc();
  45.     } else {
  46.         OpenFile(*inFileSpec);
  47.     }
  48.     mWindow->Show();
  49. }
  50.  
  51. // ---------------------------------------------------------------------------
  52. //    Respond to commands from menus, buttons, etc.
  53. Boolean
  54. CCounterDocument::ObeyCommand(CommandT    inCommand, void* ioParam)
  55. {
  56.     Boolean    cmdHandled = true;
  57.     switch (inCommand) {
  58.         case cmd_SetValue:
  59.             Int32 newValue = 0;
  60.             if (AskForValue(newValue)) {
  61.                 stCounter.SetValue(newValue);
  62.                 mCaption->SetValue(stCounter.GetValue());
  63.                 mIsModified = true;
  64.             }
  65.             break;
  66.  
  67.         case cmd_Increment:
  68. //            ::SysBeep(1);
  69.             stCounter.Increment();
  70.             mCaption->SetValue(stCounter.GetValue());
  71.             mIsModified = true;
  72.             break;
  73.  
  74.         case cmd_Decrement:
  75.             stCounter.Decrement();
  76.             mCaption->SetValue(stCounter.GetValue());
  77.             mIsModified = true;
  78.             break;
  79.  
  80.         default:
  81.             cmdHandled = LSingleDoc::ObeyCommand(inCommand, ioParam);
  82.             break;
  83.     }
  84.     return cmdHandled;
  85. }
  86.  
  87. // ---------------------------------------------------------------------------
  88. //    This function enables menu commands.
  89. void
  90. CCounterDocument::FindCommandStatus(
  91.     CommandT    inCommand,
  92.     Boolean        &outEnabled,
  93.     Boolean        &outUsesMark,
  94.     Char16        &outMark,
  95.     Str255        outName)
  96. {
  97.     switch (inCommand) {
  98.         // Return menu item status according to command messages.
  99.  
  100.         case cmd_SetValue:
  101.             outEnabled = true;            // enable the button
  102.             break;
  103.  
  104.         case cmd_Increment:
  105.             outEnabled = true;            // enable the button
  106.             break;
  107.  
  108.         case cmd_Decrement:
  109.             outEnabled = true;            // enable the button
  110.             break;
  111.  
  112.         default:
  113.             LSingleDoc::FindCommandStatus(inCommand, outEnabled,
  114.                                                 outUsesMark, outMark, outName);
  115.             break;
  116.     }
  117. }
  118.  
  119. // ---------------------------------------------------------------------------
  120. LWindow*
  121. CCounterDocument::MakeDocumentWindow()
  122. {
  123.     LWindow* theWindow = LWindow::CreateWindow(rWindow_Sample, this );
  124.     mCaption = dynamic_cast<LCaption*>(theWindow->FindPaneByID(kCaption));
  125.     ThrowIfNil_(mCaption);
  126.     theWindow->Show();
  127.     return theWindow;
  128. }
  129.  
  130. // ---------------------------------------------------------------------------
  131. Boolean
  132. CCounterDocument::AskForValue(Int32& newValue)
  133. {
  134.     Str255 numStr;
  135.     Boolean theResult = UModalDialogs::AskForOneNumber(this,
  136.                             rDialog_Value, kEditField, newValue);
  137.     return theResult;
  138. }
  139.  
  140. // ---------------------------------------------------------------------------------
  141. void
  142. CCounterDocument::NameNewDoc()
  143. {
  144.     LStr255    theTitle("\pUntitled");
  145.     // Find the first available title. We could also check the window
  146.     // pane id if we wanted to make sure we didn't collide with other
  147.     // window types.
  148.     Int32    theNumber = 0;
  149.     while (UWindows::FindNamedWindow(theTitle) != nil ) {
  150.         // An existing window has the current name
  151.         // Increment counter and try again.
  152.         ++theNumber;
  153.         theTitle = "\pUntitled ";
  154.         theTitle += static_cast<Int32>(theNumber);
  155.     }            
  156.     // Finally, set window title.
  157.     mWindow->SetDescriptor(theTitle);
  158. }
  159.  
  160. // ---------------------------------------------------------------------------------
  161. void
  162. CCounterDocument::OpenFile(FSSpec& inFileSpec)
  163. {
  164.     mFile = new LFile(inFileSpec);
  165.     ReadTheData();
  166.     mWindow->SetDescriptor(inFileSpec.name);    // set window title
  167.     mIsSpecified = true;
  168. }
  169.  
  170. // ---------------------------------------------------------------------------------
  171. void
  172. CCounterDocument::DoRevert()
  173. {
  174.     ReadTheData();
  175. }
  176.  
  177. // ---------------------------------------------------------------------------------
  178. void
  179. CCounterDocument::ReadTheData()
  180. {
  181.     mFile->OpenDataFork(fsRdWrPerm);
  182.     Int32** valueHdl = (Int32**)mFile->ReadDataFork();
  183.     mFile->CloseDataFork();
  184.     stCounter.SetValue((Int32)**valueHdl);
  185.     ::DisposeHandle((Handle)valueHdl);
  186.     mCaption->SetValue(stCounter.GetValue());    
  187.     mIsModified = false;
  188. }
  189.  
  190. // ---------------------------------------------------------------------------------
  191. void
  192. CCounterDocument::DoAESave(FSSpec& inFileSpec, OSType inFileType)
  193. {
  194.     delete mFile;    // this does nothing to the actual file on disk.
  195.     mFile = nil;
  196.     mFile = new LFile(inFileSpec);    
  197.     mIsSpecified = true;
  198.     // Get the proper file type.
  199.     OSType    theFileType = kDocType;
  200.     if (inFileType != fileType_Default) {
  201.         theFileType = inFileType;
  202.     }
  203.     mFile->CreateNewDataFile(kCreatorType, theFileType);
  204.     DoSave();
  205.     mWindow->SetDescriptor(inFileSpec.name);    // set window title
  206. }
  207.  
  208. // ---------------------------------------------------------------------------------
  209. void
  210. CCounterDocument::DoSave()
  211. {
  212.     mFile->OpenDataFork(fsRdWrPerm);
  213.     Int32 value = stCounter.GetValue();
  214.     int kBytesToWrite = sizeof(Int32);
  215.     mFile->WriteDataFork(&value, kBytesToWrite);
  216.     mFile->CloseDataFork();
  217.     mIsModified = false;
  218. }
  219.  
  220. // ---------------------------------------------------------------------------------
  221. void
  222. CCounterDocument::DoPrint()
  223. {
  224.     StDeleter<LPrintout> thePrintout(LPrintout::CreatePrintout( rPrintout ));
  225.     ThrowIfNil_(thePrintout.Get());
  226.     
  227.     // Set the print record.
  228.     if (mPrintRecordH) {
  229.         thePrintout->SetPrintRecord( mPrintRecordH );
  230.     }
  231.     
  232.     // Get the placeholder.
  233.     LPlaceHolder    *thePlaceholder;
  234.     thePlaceholder = dynamic_cast<LPlaceHolder*>
  235.                 (thePrintout->FindPaneByID(rPlaceholder));
  236.     ThrowIfNil_(thePlaceholder);
  237.     
  238.     // Install the text view in the placeholder.
  239.     thePlaceholder->InstallOccupant( mCaption, atNone );
  240.     
  241.     // Print.
  242.     thePrintout->DoPrintJob();
  243.     
  244.     // Delete the printout (handled automatically by the
  245.     // StDeleter object). The text view is returned
  246.     // to the window when the placeholder is destroyed.
  247.  
  248. }
  249.